/*
Copyright [2020] [https://www.xiaonuo.vip]

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：

1.请不要删除和修改根目录下的LICENSE文件。
2.请不要删除和修改Snowy源码头部的版权声明。
3.请保留源码和相关描述文件的项目出处，作者声明等。
4.分发源码时候，请注明软件出处 https://gitee.com/xiaonuobase/snowy
5.在修改包名，模块名称，项目代码等时，请注明软件出处 https://gitee.com/xiaonuobase/snowy
6.若您的项目无法满足以上几点，可申请商业授权，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.modular.facegrouporgrelate.controller;

import vip.xiaonuo.core.annotion.BusinessLog;
import vip.xiaonuo.core.annotion.Permission;
import vip.xiaonuo.core.enums.LogAnnotionOpTypeEnum;
import vip.xiaonuo.core.pojo.response.ErrorResponseData;
import vip.xiaonuo.core.pojo.response.ResponseData;
import vip.xiaonuo.core.pojo.response.SuccessResponseData;
import vip.xiaonuo.modular.facegrouporgrelate.param.FacegroupOrgRelateParam;
import vip.xiaonuo.modular.facegrouporgrelate.param.RelateSaveEntity;
import vip.xiaonuo.modular.facegrouporgrelate.service.FacegroupOrgRelateService;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import vip.xiaonuo.modular.grouporgrelatechangetemp.entity.GroupOrgRelateChangeTemp;
import vip.xiaonuo.modular.grouporgrelatechangetemp.param.GroupOrgRelateChangeTempParam;
import vip.xiaonuo.modular.grouporgrelatechangetemp.service.GroupOrgRelateChangeTempService;

import javax.annotation.Resource;
import java.util.List;

/**
 * 分组机构关联控制器
 *
 * @author JOE
 * @date 2022-10-10 14:04:05
 */
@RestController
public class FacegroupOrgRelateController {

    @Resource
    private FacegroupOrgRelateService facegroupOrgRelateService;

    @Resource
    private GroupOrgRelateChangeTempService tempService;

    /**
     * 查询分组机构关联
     *
     * @author JOE
     * @date 2022-10-10 14:04:05
     */
    @Permission
    @GetMapping("/facegroupOrgRelate/page")
    @BusinessLog(title = "分组机构关联_查询", opType = LogAnnotionOpTypeEnum.QUERY)
    public ResponseData page(FacegroupOrgRelateParam facegroupOrgRelateParam) {
        return new SuccessResponseData(facegroupOrgRelateService.page(facegroupOrgRelateParam));
    }

    /**
     * 添加分组机构关联
     *
     * @author JOE
     * @date 2022-10-10 14:04:05
     */
    @Permission
    @PostMapping("/facegroupOrgRelate/add")
    @BusinessLog(title = "分组机构关联_增加", opType = LogAnnotionOpTypeEnum.ADD)
    public ResponseData add(@RequestBody @Validated(FacegroupOrgRelateParam.add.class) FacegroupOrgRelateParam facegroupOrgRelateParam) {
            facegroupOrgRelateService.add(facegroupOrgRelateParam);
        return new SuccessResponseData();
    }

    /**
     * 删除分组机构关联
     *
     * @author JOE
     * @date 2022-10-10 14:04:05
     */
    @Permission
    @PostMapping("/facegroupOrgRelate/delete")
    @BusinessLog(title = "分组机构关联_删除", opType = LogAnnotionOpTypeEnum.DELETE)
    public ResponseData delete(@RequestBody @Validated(FacegroupOrgRelateParam.delete.class) FacegroupOrgRelateParam facegroupOrgRelateParam) {
            facegroupOrgRelateService.delete(facegroupOrgRelateParam);
        return new SuccessResponseData();
    }

    /**
     * 编辑分组机构关联
     *
     * @author JOE
     * @date 2022-10-10 14:04:05
     */
    @Permission
    @PostMapping("/facegroupOrgRelate/edit")
    @BusinessLog(title = "分组机构关联_编辑", opType = LogAnnotionOpTypeEnum.EDIT)
    public ResponseData edit(@RequestBody @Validated(FacegroupOrgRelateParam.edit.class) FacegroupOrgRelateParam facegroupOrgRelateParam) {
            facegroupOrgRelateService.edit(facegroupOrgRelateParam);
        return new SuccessResponseData();
    }

    /**
     * 查看分组机构关联
     *
     * @author JOE
     * @date 2022-10-10 14:04:05
     */
    @Permission
    @GetMapping("/facegroupOrgRelate/detail")
    @BusinessLog(title = "分组机构关联_查看", opType = LogAnnotionOpTypeEnum.DETAIL)
    public ResponseData detail(@Validated(FacegroupOrgRelateParam.detail.class) FacegroupOrgRelateParam facegroupOrgRelateParam) {
        return new SuccessResponseData(facegroupOrgRelateService.detail(facegroupOrgRelateParam));
    }

    /**
     * 分组机构关联列表
     *
     * @author JOE
     * @date 2022-10-10 14:04:05
     */
    @Permission
    @GetMapping("/facegroupOrgRelate/list")
    @BusinessLog(title = "分组机构关联_列表", opType = LogAnnotionOpTypeEnum.QUERY)
    public ResponseData list(FacegroupOrgRelateParam facegroupOrgRelateParam) {
        return new SuccessResponseData(facegroupOrgRelateService.list(facegroupOrgRelateParam));
    }

    /**
     * 保存分组机构关联关系
     * @param saveEntity
     * @return
     */
    @Permission
    @PostMapping("/facegroupOrgRelate/save")
    @BusinessLog(title = "分组机构关联_保存", opType = LogAnnotionOpTypeEnum.UPDATE)
    public ResponseData save(@RequestBody RelateSaveEntity saveEntity) {
        Long groupId = saveEntity.getFaceGroupId();
        GroupOrgRelateChangeTempParam tempParam = new GroupOrgRelateChangeTempParam();
        tempParam.setStatus(0);
        tempParam.setFaceGroupId(groupId);
        synchronized (FacegroupOrgRelateController.class){
            List<GroupOrgRelateChangeTemp> list = tempService.list(tempParam);
            if(list==null || list.size()==0){
                return new SuccessResponseData(facegroupOrgRelateService.saveRelate(saveEntity));
            }else{
                return new ErrorResponseData(1,"当前分组存在未处理完成的分组转移数据，请稍后再保存");
            }
        }

    }

}
